package demo.cxf.app;

import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import demo.cxf.service.HelloWorld;

public class ClientTest {
	private String url = "http://localhost:8080/CXFDemo/services/HelloWorld";
	private String wsdl = url + "?wsdl";

	/**
	 * WITHOUT SEI
	 * 
	 * @throws Exception
	 */
	@Test
	public void testJaxWsDynamicClientFactory() throws Exception {
		JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
		Client client = clientFactory.createClient(wsdl);
		Object[] result = client.invoke("sayHi", "KEVIN");
		System.out.println(result[0]);

		Object customer = Thread.currentThread().getContextClassLoader().loadClass("cn.com.cnet.Customer").newInstance();
		Method m = customer.getClass().getMethod("setName", String.class);
		m.invoke(customer, "Joe Schmoe");
		Object[] result2 = client.invoke("sayNothing", customer);
		System.out.println(result2[0]);

	}

	/**
	 * NEED SEI
	 */
	@Test
	public void testJaxWsProxyFactoryBean() {
		JaxWsProxyFactoryBean bean = new JaxWsProxyFactoryBean();
		bean.setServiceClass(HelloWorld.class);
		bean.setAddress(url);
		HelloWorld helloWorldService = (HelloWorld) bean.create();
		String result = helloWorldService.sayHi("Kevin");
		System.out.println(result);

	}

	/**
	 * NEED SEI
	 * @throws Exception 
	 */
	@Test
	public void testSpringClient() throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "spring-client.xml" });
		HelloWorld client = (HelloWorld) context.getBean("client");
		String response = client.sayHi("Joe");
		System.out.println("Response: " + response);
		System.out.println("===========================");
		Client client2 = (Client) context.getBean("dynClient");
		Object[] result = client2.invoke("sayHi", "KEVIN");
		System.out.println("Response: " +result[0]);
	}
	
	
	/**
	 * NEED SEI
	 */
	@Test
	public void testServiceClient() throws MalformedURLException {
		URL wsdlURL = new URL(wsdl);
		QName SERVICE_NAME = new QName("http://www.cnet.com.cn", "HelloWorldImplService");
		Service service = Service.create(wsdlURL, SERVICE_NAME);
		HelloWorld client = service.getPort(HelloWorld.class);
		String result = client.sayHi("test");
		System.out.println(result);
	}

	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "spring-client.xml" });
		HelloWorld client = (HelloWorld) context.getBean("client");
		String response = client.sayHi("Joe");
		System.out.println("Response: " + response);
	}
}